package ext.generic.erp.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;

import ext.generic.erp.bean.ERPPartTemplateBean;
import ext.generic.erp.common.ExcelUtil;
import ext.generic.erp.util.IntegrationConstant;
import ext.generic.erp.util.PDMIntegrationLogUtil;

public class ERPPartTemplateConfigExcelUtil {
	private static String clazz = ERPPartTemplateConfigExcelUtil.class.getName() ;
	
	private static final String CONFIG_FILE = "ERPPartTemplate.xls" ;
	
	private static final int RAW_MATERIAL_SHEET_INDEX = 0 ;
	
	private static final int SEMI_FINISHED_GOOD_SHEET_INDEX = 1 ;
	
	private static final int FINISHED_GOOD_SHEET_INDEX = 2 ;
	
	private List<ERPPartTemplateBean> rawMaterialList = new ArrayList<ERPPartTemplateBean>() ;
	
	private List<ERPPartTemplateBean> semiFinishedGoodList = new ArrayList<ERPPartTemplateBean>() ;
	
	private List<ERPPartTemplateBean> finishedGoodList = new ArrayList<ERPPartTemplateBean>() ;
	
	private static final ERPPartTemplateConfigExcelUtil erpPartTemplateUtilSingleton = new ERPPartTemplateConfigExcelUtil();
	
	private ERPPartTemplateConfigExcelUtil(){
		parseExcel() ;
	}
	
	public static ERPPartTemplateConfigExcelUtil getInstance() {
        return erpPartTemplateUtilSingleton ;
    }
	
	public List<ERPPartTemplateBean> getRawMaterialList() {
		return rawMaterialList;
	}

	public List<ERPPartTemplateBean> getSemiFinishedGoodList() {
		return semiFinishedGoodList;
	}

	public List<ERPPartTemplateBean> getFinishedGoodList() {
		return finishedGoodList;
	}
	
	private void parseExcel(){
		//配置文件与ext.generic.erp.config.ERPPartTemplateUtil.class文件在同一路径下
		String filePath = ERPPartTemplateConfigExcelUtil.class.getResource("").getPath() ;
		
		//BUG Fix，解决Linux下文件路径中windchill小写不能找到文件的BUG
		filePath = filePath.replaceAll("windchill", "Windchill") ;
		
		filePath = filePath + CONFIG_FILE ;
		
		PDMIntegrationLogUtil.printLog(clazz, "ERP Part Template File Path : " + filePath , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
		Workbook book = ExcelUtil.getWorkbook( filePath );
		
		if( book == null ){
			PDMIntegrationLogUtil.printLog(clazz, "parseExcel() Method , book == null " , IntegrationConstant.LOG_LEVEL_ERROR ) ;		
			return ;
		}else{
			parseExcel(book , RAW_MATERIAL_SHEET_INDEX , rawMaterialList ) ;
			parseExcel(book , SEMI_FINISHED_GOOD_SHEET_INDEX , semiFinishedGoodList ) ;
			parseExcel(book , FINISHED_GOOD_SHEET_INDEX , finishedGoodList ) ;
		}
	}
	
	private void parseExcel(Workbook book , int sheetIndex , List<ERPPartTemplateBean> list ){
		Sheet sheet = book.getSheetAt( sheetIndex ) ;
		
		if( sheet == null ){
			PDMIntegrationLogUtil.printLog(clazz, "parseExcel(Workbook book , int sheetIndex , List<ERPPartTemplateBean> list ) Method , sheet == null " , IntegrationConstant.LOG_LEVEL_ERROR ) ;
			return ;
		}
		
		Iterator<Row> rowIte = sheet.iterator() ;
		
		//跳过表头
		if( rowIte.hasNext() ){
			rowIte.next() ;
		}
		
		while( rowIte.hasNext() ){
			Row row = rowIte.next() ;
			
			if( row != null ){
				String pdmPartCode = ExcelUtil.getCellValueByIndex(row, 0 , true) ;
				String pdmPartENameOrSource = ExcelUtil.getCellValueByIndex(row, 1 , false) ;
				String erpPartCategory = ExcelUtil.getCellValueByIndex(row, 2 , false) ;
				String erpPartTemplate = ExcelUtil.getCellValueByIndex(row, 3 , false) ;
				
				ERPPartTemplateBean erpPartTemplateBean = new ERPPartTemplateBean() ;
				
				erpPartTemplateBean.setRowIndex(row.getRowNum()+"") ;
				erpPartTemplateBean.setPdmPartCode( pdmPartCode ) ;
				erpPartTemplateBean.setPdmPartENameOrSource( pdmPartENameOrSource ) ;
				erpPartTemplateBean.setErpPartCategory( erpPartCategory ) ;
				erpPartTemplateBean.setErpPartTemplate( erpPartTemplate ) ;
				
				list.add( erpPartTemplateBean ) ;
			}
		}
	}
	
	private static void printList( List<ERPPartTemplateBean> list ){
		if( list == null ){
			System.out.println("list == null") ;
			return ;
		}
		
		Iterator<ERPPartTemplateBean>  ite = list.iterator() ;
		
		while( ite.hasNext() ){
			ERPPartTemplateBean erpPartTemplateBean = ite.next() ;
			
			System.out.println(erpPartTemplateBean) ;
		}
	}
	
	public static void main(String[] args) {
		System.out.println(">>>>> 原材料ERP物料类别和模板映射表信息如下：") ;
		printList( ERPPartTemplateConfigExcelUtil.getInstance().getRawMaterialList() ) ;
		
		System.out.println(">>>>> 半成品ERP物料类别和模板映射表信息如下：") ;
		printList( ERPPartTemplateConfigExcelUtil.getInstance().getSemiFinishedGoodList() ) ;
		
		System.out.println(">>>>> 成品ERP物料类别和模板映射表信息如下：") ;
		printList( ERPPartTemplateConfigExcelUtil.getInstance().getFinishedGoodList() ) ;
	}
}
